﻿#pragma execution_character_set("utf-8")

#include "win_main.h"
#include "ui_win_main.h"
#include "core_helper/iconhelper.h"
#include "core_helper/qthelper.h"
#include "qsplitter.h"
#include <QDebug>

mainWindow *g_mainWindow = nullptr;

mainWindow::mainWindow(QWidget *parent) : FramelessWidget(parent), ui(new Ui::mainWindow)
{
    g_mainWindow = this;
    this->initStyle();
    ui->setupUi(this);
    this->initForm();

    this->initLeftMain();
    this->initLeftConfig();
}

mainWindow::~mainWindow()
{
    delete ui;
}

//bool mainWindow::eventFilter(QObject *watched, QEvent *event)
//{
//    if (watched == ui->widgetTitle) {
//        if (event->type() == QEvent::MouseButtonDblClick) {
//            on_btnMenu_Max_clicked();
//        }
//    }
//    return QWidget::eventFilter(watched, event);
//}

void mainWindow::keyPressEvent(QKeyEvent *ev)
{
    bool needKey = true;
    if(this->isMaximized())
    {
        if(!ui->widgetTitle->isVisible() && ev->key() == Qt::Key_Escape)
        {
            on_fullScreen(false);
            needKey = false;
        }
    }
    if (needKey)
    {
        QWidget::keyPressEvent(ev);
    }
}

void mainWindow::initForm()
{
    //设置无边框
    //QtHelper::setFramelessForm(this);

    this->setWindowTitle(ui->labTitle->text());
    this->setTitleBar(ui->widgetTitle);

    ui->widgetTitle->setProperty("form", "title");
    ui->widgetTop->setProperty("nav", "top");

        //关联信号
    connect(this, SIGNAL(titleDblClick()), this, SLOT(on_titleDblClick()));
    connect(this, SIGNAL(windowStateChange(bool)), this, SLOT(on_windowStateChange(bool)));

    //设置图标
    IconHelper::setIcon(ui->btnMenu_Min, 0xf068);
    IconHelper::setIcon(ui->btnMenu_Max, 0xf067);
    IconHelper::setIcon(ui->btnMenu_Close, 0xf00d);


    //ui->stackedWidget->setStyleSheet("QLabel{font:60px;}");

    QSize icoSize(32, 32);
    int icoWidth = 85;

    //设置顶部导航按钮
    QList<QAbstractButton *> tbtns = ui->widgetTop->findChildren<QAbstractButton *>();
    foreach (QAbstractButton *btn, tbtns) {
        btn->setIconSize(icoSize);
        btn->setMinimumWidth(icoWidth);
        btn->setCheckable(true);
        //connect(btn, SIGNAL(clicked()), this, SLOT(buttonClick()));
    }

    ui->btnMenu_real->click();

    //ui->deviceList_page->setProperty("flag", "left");
    //ui->widgetLeftConfig->setProperty("flag", "left");
    //ui->page1->setStyleSheet(QString("QWidget[flag=\"left\"] QAbstractButton{min-height:%1px;max-height:%1px;}").arg(60));
    //ui->page2->setStyleSheet(QString("QWidget[flag=\"left\"] QAbstractButton{min-height:%1px;max-height:%1px;}").arg(25));
}

void mainWindow::initStyle()
{
    //加载样式表
    QString qss = QtHelper::getStyle(":/qss/lightblue.css");
    if (!qss.isEmpty()) {
        QString paletteColor = qss.mid(20, 7);
        qApp->setPalette(QPalette(QColor(paletteColor)));
        qApp->setStyleSheet(qss);
    }
}

void mainWindow::buttonClick()
{
    QAbstractButton *b = (QAbstractButton *)sender();
    //QString name = b->text();
    QString name = b->objectName();

    QList<QAbstractButton *> tbtns = ui->widgetTop->findChildren<QAbstractButton *>();
    foreach (QAbstractButton *btn, tbtns) {
        btn->setChecked(btn == b);
    }

    if (name == "btnMenu_real") {
        ui->stackedWidget->setCurrentIndex(0);
    } else if (name == "btnMenu_playback") {
        ui->stackedWidget->setCurrentIndex(0);
    } else if (name == "btnMenu_download") {
        ui->stackedWidget->setCurrentIndex(1);
    } else if (name == "btnMenu_appcenter") {
        ui->stackedWidget->setCurrentIndex(2);
    }
}

void mainWindow::initLeftMain()
{
//    iconsMain << 0xf030 << 0xf03e << 0xf247;
//    btnsMain << ui->tbtnMain1 << ui->tbtnMain2 << ui->tbtnMain3;

//    for (int i = 0; i < btnsMain.count(); ++i) {
//        QToolButton *btn = (QToolButton *)btnsMain.at(i);
//        btn->setCheckable(true);
//        btn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
//        connect(btn, SIGNAL(clicked(bool)), this, SLOT(leftMainClick()));
//    }

//    IconHelper::StyleColor styleColor;
//    styleColor.position = "left";
//    styleColor.iconSize = 18;
//    styleColor.iconWidth = 35;
//    styleColor.iconHeight = 25;
//    styleColor.borderWidth = 4;
//    styleColor.borderColor = borderColor;
//    styleColor.setColor(normalBgColor, normalTextColor, darkBgColor, darkTextColor);
//    IconHelper::setStyle(ui->widgetLeftMain, btnsMain, iconsMain, styleColor);
//    ui->tbtnMain1->click();
}

void mainWindow::initLeftConfig()
{
    iconsConfig << 0xf031 << 0xf036 << 0xf249 << 0xf055 << 0xf05a << 0xf249;
    btnsConfig << ui->tbtnConfig1 << ui->tbtnConfig2 << ui->tbtnConfig3 << ui->tbtnConfig4 << ui->tbtnConfig5 << ui->tbtnConfig6;

    for (int i = 0; i < btnsConfig.count(); ++i) {
        QToolButton *btn = (QToolButton *)btnsConfig.at(i);
        btn->setCheckable(true);
        btn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        connect(btn, SIGNAL(clicked(bool)), this, SLOT(leftConfigClick()));
    }

//    IconHelper::StyleColor styleColor;
//    styleColor.position = "left";
//    styleColor.iconSize = 16;
//    styleColor.iconWidth = 20;
//    styleColor.iconHeight = 20;
//    styleColor.borderWidth = 3;
//    styleColor.borderColor = borderColor;
//    styleColor.setColor(normalBgColor, normalTextColor, darkBgColor, darkTextColor);
//    IconHelper::setStyle(ui->widgetLeftConfig, btnsConfig, iconsConfig, styleColor);
//    ui->tbtnConfig1->click();
}

void mainWindow::leftMainClick()
{
    QAbstractButton *b = (QAbstractButton *)sender();
    QString name = b->text();
//    for (int i = 0; i < btnsMain.count(); ++i) {
//        QAbstractButton *btn = btnsMain.at(i);
//        btn->setChecked(btn == b);
//    }

    //ui->lab1->setText(name);
}

void mainWindow::leftConfigClick()
{
    QToolButton *b = (QToolButton *)sender();
    QString name = b->text();
    for (int i = 0; i < btnsConfig.count(); ++i) {
        QAbstractButton *btn = btnsConfig.at(i);
        btn->setChecked(btn == b);
    }

    ui->lab2->setText(name);
}

void mainWindow::on_btnMenu_Min_clicked()
{
#ifdef Q_OS_MACOS
    this->setWindowFlags(this->windowFlags() & ~Qt::FramelessWindowHint);
#endif
    this->showMinimized();
}

void mainWindow::on_btnMenu_Max_clicked()
{
    if (this->isMaximized()) {
        this->showNormal();
        ui->btnMenu_Max->setToolTip("最大");
    } else {
        this->showMaximized();
        ui->btnMenu_Max->setToolTip("还原");
    }
}

void mainWindow::on_btnMenu_Close_clicked()
{
    close();
}

void mainWindow::on_titleDblClick()
{
    on_btnMenu_Max_clicked();
}
void mainWindow::on_windowStateChange(bool max)
{
    ui->btnMenu_Max->setToolTip(max ? tr("还原") : tr("最大"));
}
void mainWindow::on_fullScreen(bool full)
{
    //qDebug() << "on_fullScreen" << full;
    if (full)
    {
        this->showMaximized();
        ui->widgetTitle->setVisible(false);
        ui->page_video->fullScreen(full);
    }
    else
    {
        ui->widgetTitle->setVisible(true);
        ui->page_video->fullScreen(full);
        this->showNormal();
    }
}

void mainWindow::on_btnMenu_real_clicked()
{
    QAbstractButton *b = (QAbstractButton *)sender();
    QList<QAbstractButton *> tbtns = ui->widgetTop->findChildren<QAbstractButton *>();
    foreach (QAbstractButton *btn, tbtns) {
        btn->setChecked(btn == b);
    }
    ui->stackedWidget->setCurrentIndex(0);
    ui->page_video->setRealtime(true);
}
void mainWindow::on_btnMenu_playback_clicked()
{
    QAbstractButton *b = (QAbstractButton *)sender();
    QList<QAbstractButton *> tbtns = ui->widgetTop->findChildren<QAbstractButton *>();
    foreach (QAbstractButton *btn, tbtns) {
        btn->setChecked(btn == b);
    }
    ui->stackedWidget->setCurrentIndex(0);
    ui->page_video->setRealtime(false);
}

void mainWindow::on_btnMenu_download_clicked()
{
    QAbstractButton *b = (QAbstractButton *)sender();
    QList<QAbstractButton *> tbtns = ui->widgetTop->findChildren<QAbstractButton *>();
    foreach (QAbstractButton *btn, tbtns) {
        btn->setChecked(btn == b);
    }
    ui->stackedWidget->setCurrentIndex(1);
}
void mainWindow::on_btnMenu_appcenter_clicked()
{
    QAbstractButton *b = (QAbstractButton *)sender();
    QList<QAbstractButton *> tbtns = ui->widgetTop->findChildren<QAbstractButton *>();
    foreach (QAbstractButton *btn, tbtns) {
        btn->setChecked(btn == b);
    }
    ui->stackedWidget->setCurrentIndex(2);
}
